package com.netty.bfport.http.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netty.bfport.http.common.utils.*;
import com.netty.bfport.http.domain.dto.UserLoginDTO;
import com.netty.bfport.http.mapper.UserMapper;
import com.netty.bfport.http.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Author: zhaoyang05
 * @Data: 2022/3/29 21:42
 * @Description
 */
@Service
public class UserServiceImpl implements UserService {

    private static final String CODE = "login_user:";
    private static final String NAME = "fz_";
    @Value("${user.header}")
    private String header;

    @Resource
    RedisTemplate<String,Object> redisTemplate;
    @Resource
    UserMapper userMapper;
    @Resource
    TokenUtil tokenUtil;

    @Override
    public JSONObject sign(UserLoginDTO userDTO, HttpServletRequest request){
//    Integer onCode = (Integer)redisTemplate.boundValueOps(CODE+userDTO.getTelephone()).get();
//        assert onCode != null;
//        if (!onCode.equals(userDTO.getCode())){
//        return "0";    //验证码错误
//    }
        JSONObject json = new JSONObject();
    String telephone = userDTO.getTelephone();
    String idByPhone = userMapper.findIdByPhone(telephone);
        if (StringUtils.isNotBlank(idByPhone)){
            UserLoginDTO userDO = userMapper.findUserDO(idByPhone);
            json = JSONObject.parseObject(JSON.toJSONString(userDO));
            json.put("token",base64To(AesEncodeUtil.encrypt(tokenUtil.makeTokenInRedis(idByPhone, telephone, RequestUtil.getIpAddress(request)))));
            return json; //用户已注册
    }
    String uuid = UuidUtils.getUUID();
        UserLoginDTO userDO = new UserLoginDTO();
        BeanUtils.copyProperties(userDTO,userDO);
        userDO.setId(uuid);
        userDO.setUsername(NAME + UuidUtils.getUUID());
        userDO.setHeader(header);
        userDO.setCreateTime(new Date());
        userDO.setPassword(SHA1.getSha1(userDO.getPassword()));
    Integer sign = userMapper.sign(userDO);
        if (sign<=0){
            json.put("token","1");
//        return "1";  //添加失败
    }
        json = JSONObject.parseObject(JSON.toJSONString(userDO));
        json.put("token",base64To(AesEncodeUtil.encrypt(tokenUtil.makeTokenInRedis(uuid, telephone, RequestUtil.getIpAddress(request)))));
        return json;
}



    @Override
    public JSONObject loginPas(@Validated UserLoginDTO userDTO, HttpServletRequest request) {
        UserLoginDTO userDO = new UserLoginDTO();
        BeanUtils.copyProperties(userDTO,userDO);
        userDO.setPassword(SHA1.getSha1(userDO.getPassword()));
        JSONObject login = userMapper.login(userDO);
//        if (StringUtils.isBlank(login)){
//            return "0";
//        }
        if (login==null){
            login = new JSONObject();
            login.put("token","0");
            return login;
        }
        String encrypt = AesEncodeUtil.encrypt(tokenUtil.makeTokenInRedis(login.getString("id"), userDTO.getTelephone(), RequestUtil.getIpAddress(request)));
        login.put("token",base64To(encrypt));
        return login;
    }

    @Override
    public JSONObject tokenAnalysis(String token) {
        String encrypt = AesEncodeUtil.decrypt(token);
        return tokenUtil.tokenAnalysis(encrypt);

    }


    /**
     * 将Base64的换行符换成#号
     * */
    private String base64To(String base){
        return base.replaceAll("\r\n|\n", "#");
    }


}
